package com.leaves.contoller;

import cn.dev33.satoken.stp.StpUtil;
import com.leaves.common.constant.TokenConstant;
import com.leaves.common.publicmethod.entity.PageEntity;
import com.leaves.entity.dto.AddUserInfoDto;
import com.leaves.entity.dto.DisableUser;
import com.leaves.response.ApiResult;
import com.leaves.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Author LEAVES
 * @Date 2022/4/29 23:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/v2/user")
public class UserController {

    @Resource
    private UserInfoService userInfoService;

    /**
     * 查询登录状态
     *
     * @return
     */
    @RequestMapping("/loginFlag")
    public ApiResult isLogin() {
        StpUtil.checkLogin();
        return ApiResult.successData(StpUtil.isLogin());
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public ApiResult logout(HttpServletRequest request) {
        String authorization = request.getHeader(TokenConstant.AUTHORIZATION);
        String device = request.getHeader(TokenConstant.CUSTOMIZE_CLIENT_TYPE);
        String token = StringUtils.isNoneBlank(authorization) ? authorization.substring(7) : null;
        String loginId = (String) StpUtil.getLoginIdByToken(token);
        StpUtil.logout(loginId, device);
        return ApiResult.success("退出成功");
    }

    // 查询 Token 信息
//    @SaCheckLogin
    @RequestMapping("/tokenInfo")
    public ApiResult tokenInfo(HttpServletRequest request) {
        return ApiResult.successData(StpUtil.getTokenInfo());
    }

    /**
     * 获取当前账号信息
     *
     * @return
     */
//    @SaCheckLogin
    @RequestMapping("/currentAccount")
    public ApiResult getCurrentAccountInformation() {
        return userInfoService.getCurrentAccountInformation();
    }

    /**
     * 分页查询用户信息列表
     *
     * @param pageEntity
     * @return
     */
//    @SaCheckLogin     //跳过登录注解
    @RequestMapping("/page/list")
    public ApiResult pageQueryUserInfoList(@Validated PageEntity pageEntity) {
        return userInfoService.pageQueryUserInfoList(pageEntity);
    }

    /**
     * 账号封禁
     *
     * @param disableUser
     * @return
     */
    @PostMapping("/disable")
    public ApiResult disableUserInfo(@Validated DisableUser disableUser) {
        return userInfoService.disableUserInfo(disableUser);
    }

    /**
     * 手动解封账号
     *
     * @param userId
     * @return
     */
    @PostMapping("/untie/disable")
    public ApiResult untieDisableUserInfo(@RequestParam("userId") String userId) {
        return userInfoService.untieDisableUserInfo(userId);
    }

    /**
     * 新增账号信息
     *
     * @param addUserInfoDto
     * @return
     */
    @PostMapping("/save")
    public ApiResult saveUserInfo(@Valid AddUserInfoDto addUserInfoDto) {
        return userInfoService.AddUserInfoDto(addUserInfoDto);
    }


}

